package org.deltaset.meta.testdomain.service;

import java.util.List;
import org.deltaset.meta.annotation.DsAcceptEntity;
import org.deltaset.meta.annotation.DsDefaultFinder;
import org.deltaset.meta.annotation.DsFactoryMethod;
import org.deltaset.meta.annotation.DsFirstResult;
import org.deltaset.meta.annotation.DsMaxResult;
import org.deltaset.meta.annotation.DsReturnEntity;

/**
 *
 * @author dmakariev
 */
public class BaseModelRepository implements ModelRepository {

    private final BaseRepository baseRepository;
    private final Class currentClass;

    BaseModelRepository(BaseRepository baseRepository, Class entityClass) {
        this.baseRepository = baseRepository;
        this.currentClass = entityClass;
    }

    @DsFactoryMethod
    @DsReturnEntity
    @Override
    public Object newEntity() {
        return baseRepository.newEntity(currentClass);
    }

    @Override
    public Long countAll() {
        return baseRepository.countAll(currentClass);
    }

    @DsAcceptEntity
    @Override
    public void warnDelete(Object entity) {
        final Object id = EntityResolverImpl.extractEntityIdInternal(entity);
        baseRepository.delete(currentClass, id);
    }

    @DsDefaultFinder
    @Override
    public List findAll(@DsFirstResult Integer firstResult, @DsMaxResult Integer maxResult) {
        return baseRepository.findAll(currentClass, firstResult, maxResult);
    }

//    @Override
//    public List findAll() {
//        return baseRepository.findAll(currentClass, 0, 500);
//    }
    @DsReturnEntity
    @Override
    public Object findById(Object id) {
        return baseRepository.findById(currentClass, id);
    }

    @DsAcceptEntity
    @Override
    public void save(Object entity) {
        baseRepository.save(entity);
    }

    @DsAcceptEntity
    @DsReturnEntity
    @Override
    public Object edit(Object entity) {
        return entity;
    }

    @Override
    public boolean disableEdit(Object entity) {
        final Object id = EntityResolverImpl.extractEntityIdInternal(entity);
        final boolean disabledEdit = (null == id);
        return disabledEdit;
    }

    @Override
    public boolean disableWarnDelete(Object entity) {
        final Object id = EntityResolverImpl.extractEntityIdInternal(entity);
        final boolean disabledDelete = (null == id);
        return disabledDelete;
    }
}
